package com.bsg.upm.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bsg.upm.annotation.Auth;
import com.bsg.upm.constant.UserRoleConstants;
import com.bsg.upm.exception.APIException;
import com.bsg.upm.http.RespJson;
import com.bsg.upm.http.RespJsonFactory;
import com.bsg.upm.service.PortService;

/**
 * 端口管理控制层
 * 
 * @author HCK
 *
 */
@Auth(value = { UserRoleConstants.ADMIN })
@Controller
@RequestMapping("/v1.0/ports")
public class PortController {

	@Resource
	private PortService portService;

	/**
	 * 【查询】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public RespJson listPort(@RequestParam Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			Map<String, Object> searchParamMap = new HashMap<String, Object>();
			if (paramMap.containsKey("site")) {
				searchParamMap.put("siteId", paramMap.get("site"));
			}
			if (paramMap.containsKey("limit")) {
				searchParamMap.put("limit", Integer.parseInt((String) paramMap.get("limit")));
			}
			if (paramMap.containsKey("start")) {
				searchParamMap.put("start", Integer.parseInt((String) paramMap.get("start")));
			}
			if (paramMap.containsKey("end")) {
				searchParamMap.put("end", Integer.parseInt((String) paramMap.get("end")));
			}
			if (paramMap.containsKey("used")) {
				searchParamMap.put("used", paramMap.get("used"));
			}
			respJson = portService.list(searchParamMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【新增】
	 * 
	 * @param paramMap
	 *            请求参数
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.POST)
	@ResponseBody
	public RespJson savePort(@RequestBody Map<String, Object> paramMap, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = portService.save(paramMap);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【启用】支持批量启用
	 * 
	 * @param siteId
	 *            站点编码
	 * @param portValues
	 *            端口集合
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "enable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson enablePort(@RequestParam(value = "site", required = true) String siteId,
			@RequestBody(required = true) int[] portValues, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = portService.enable(siteId, portValues, true);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【停用】支持批量停用
	 * 
	 * @param siteId
	 *            站点编码
	 * @param portValues
	 *            端口集合
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(value = "/disable", method = RequestMethod.PUT)
	@ResponseBody
	public RespJson disablePort(@RequestParam(value = "site", required = true) String siteId,
			@RequestBody(required = true) int[] portValues, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = portService.enable(siteId, portValues, false);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

	/**
	 * 【删除】支持批量删除
	 * 
	 * @param siteId
	 *            站点编码
	 * @param portValues
	 *            端口集合
	 * @param response
	 *            HttpServletResponse对象
	 * @return RespJson请求结果对象
	 */
	@RequestMapping(method = RequestMethod.DELETE)
	@ResponseBody
	public RespJson removePort(@RequestParam(value = "site", required = true) String siteId,
			@RequestBody(required = true) int[] portValues, HttpServletResponse response) {
		RespJson respJson = null;
		try {
			respJson = portService.remove(siteId, portValues);
			response.setStatus(respJson.getStatus());
		} catch (APIException e) {
			response.setStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR);
			respJson = RespJsonFactory.build(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
		}
		return respJson;
	}

}
